/**
*\file		test.c
*\brief		This file is test for C languange about some question whic i dont 
*			understand.
*
*		
*
*\author	Aleon.jin
*\time	2008.7.14
*/
#include <sys/types.h>
#include <sys/times.h>
#include <sys/select.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdarg.h>
#include <string.h>




static int read_key(void)
{
//#if defined(HAVE_TERMIOS_H)
    int n = 1;
    unsigned char ch;
//#ifndef CONFIG_BEOS_NETSERVER
    struct timeval tv;
    fd_set rfds;

    FD_ZERO(&rfds);
    FD_SET(0, &rfds);
    tv.tv_sec = 0;
    tv.tv_usec = 0;
    n = select(1, &rfds, NULL, NULL, &tv);
//#endif
    if (n > 0) {
        n = read(0, &ch, 1);
        if (n == 1)
            return ch;

        return n;
    }
//#elif defined(HAVE_CONIO_H)
    //if(kbhit())
       // return(getch());
//#endif
    return -1;
}
int read_key1(void)
{
	unsigned char ch;
	struct timeval time_val;
	fd_set	read_fds;

	FD_ZERO(&read_fds);
	FD_SET(0,&read_fds);
	int n=0;
	time_val.tv_sec = 0;
    	time_val.tv_usec = 0;
	n=select(1,&read_fds,NULL,NULL,&time_val);
	//n=FD_ISSET(0,&read_fds);
	//if(n>0)
		//printf("keyboard can be read\n");
	if(n){
		n = read(0, &ch, 1);
	        if(n==1)
	        	{
	        	printf("The char is %c \n", ch);
			return 1;
	        	}
	}
};

	return 0;
	
}
/*
int read_key2(void)
{
	if(kbhit())
        return(getch());
}
*/
	}

 
typedef struct {
    const char *name;	///< option name
    int flags;
    #define HAS_ARG    0x0001
#define OPT_BOOL   0x0002
#define OPT_EXPERT 0x0004
#define OPT_STRING 0x0008
#define OPT_VIDEO  0x0010
#define OPT_AUDIO  0x0020
#define OPT_GRAB   0x0040
#define OPT_INT    0x0080
#define OPT_FLOAT  0x0100
#define OPT_SUBTITLE 0x0200
#define OPT_FUNC2  0x0400
#define OPT_INT64  0x0800
#define OPT_EXIT   0x1000
     union {
        void (*func_arg)(const char *); //FIXME passing error code as int return would be nicer then exit() in the func
        int *int_arg;
        char **str_arg;
        float *float_arg;
        int (*func2_arg)(const char *, const char *);
        int64_t *int64_arg;
    } u;
    const char *help;
    const char *argname;
} OptionDef;


void main()
{
	while(1){
			printf("*****Sub thread function running\n");
			sleep(1);
		}
}


unsigned char log_level=1;
void av_log( int level , const char* fmt , ... )
{
	va_list	vl;
	int print_prefix=1;
	
	va_start( vl , fmt );
	if (level > log_level )	return;

	print_prefix=(void*)strstr(fmt,"x")!=NULL;
	vfprintf(stderr , fmt , vl);	
	
	va_end ( vl );
}


#define STRING_LENGTH	256
typedef char TestString_t[STRING_LENGTH];



int main(int* argc,char* argv[])
{
	/* test argv and pointer */
	#if 0
	char* params[]={"jinyongbin","jinfan","qinhua",NULL};
	char** temp=params;						//params is not a variable
	while(*temp)
		printf("%s\n",*temp++);

	printf("\n### ARGC ###\n%d\n", argc);
	printf("\n### ARGV ###\n");
	while(*argv)
		printf("%s\n", *(argv++));	//argv is a variable
	
	printf("\n### ENVP ###\n");
	while(*envp)
		printf("%s\n",*envp++);			//print all envp string
	while(*envp[0])								//print all character of first string
		printf("%c",*envp[0]++);
	printf("\n");
	while(*envp[1])								//print all character of second string
		printf("%c",*(envp[1])++);
	#endif
	
	/* test little end mode*/
	#if 0
	int a=0x12345678;
	unsigned char* pa=(char*)&a;
	printf("*pa=%x\n",*pa++);
	printf("*pa=%x\n",*pa++);
	printf("*pa=%x\n",*pa++);
	printf("*pa=%x\n",*pa);
	#endif
	/*test regular expression*/
	#if 0
	#include <regex.h>
	const char *string[5]={"yong","bin.jin","@gmail.com","324","9984skek"};
	const char* str;
	regex_t regex;
	const char* pattern="[a-z]*$";
	int i,j,ret,cflags=0,nm;
	regmatch_t pm[8];

	ret=regcomp(&regex, pattern, cflags);
	if (ret<0)
		printf("Err@line %d",__LINE__ );
	
	for(i=0;i<5;i++){
		ret=regexec(&regex, string[i], 1, pm, 0);
		if(ret==REG_NOMATCH)
			printf("info@line %d:expression not match\n",__LINE__);
		else if (ret==0){
			//for(j=pm[0].rm_so;j<pm[0].rm_eo;++j)
				//printf("%c",*(string[i]+j));
				//printf("\n");
						
		}
	}
	regfree(&regex);
	return 0;
	
	#endif
	
	/* test the size of variable type */
	#if 0
	printf("Bytes of char %d\t""Bytes of short %d\t""Bytes of int %d\n",sizeof(char),sizeof(short),sizeof(int));
	printf("Bytes of long %d\t""Bytes of long long %d\n",sizeof(long),sizeof(long long));
	#endif
	/* test variable arguments function*/
	#if 0
	int i=23;
	char *name="jinfan";
	#define debug_print(fmt,args...) fprintf(stderr,fmt,##args)
	debug_print("this is testing varible argument macro and the function of ##.\n");
	#endif
	
	/* test #define trace(x) printf x */	
	#if 0
	int i=1;
	#define trace(x) 	printf x
	trace(("This is %dth test line\n",i));	
	#endif
	
	/* Test chang color */
	#if 0
	 printf("\33[31m<Error@%s:%d>\33[0m\n\tThis line is a test line\n",__FILE__,__LINE__);
	 #endif
	 
	/* test typedef char TestString[STRING_LENGTH]
		It defines char array type , str is a left value and point to the first value of this array*/
	#if 0
	  TestString_t str[]={"jinfang","qinhua"};
	  unsigned int pStr;
	  pStr=(unsigned int)&str[1];
	  //strcpy(str,"jinfan");
	  printf("size of str is %d,str[0] is %s ,str[1] is %s\n",sizeof(str[0]),str[0],str[1]);	
	  printf("%s\n",(char*)pStr++);
	  printf("%s\n",(char*)pStr);
	#endif
	
	/*test struct union #define compined structure */
	#if 0
	OptionDef o;
	printf("%d bytes\n",sizeof(OptionDef));
	printf("%d bytes\n",sizeof(o.u));
	#endif
	
	/*test select funtion */
	#if 0
	while(1){
		if(read_key1())		
			printf("read char success\n");
		
		//printf("loop running");
	}
	
	OptionDef op;
	const char* str;
	int flags,*pflag;
	
	
	printf("size of OptionDef is %d\n size of u is %d\n size of int* is %d\n"
				"size of char* is %d\n size of funtion pointer is %d\n"
				"test string 1\n"
				"test string 2\n",
		sizeof(OptionDef),sizeof(op.u),sizeof(pflag),sizeof(str),sizeof(op.u.func_arg));
	#endif
	
	/*	test pthread_create */
	#if 0	
	pthread_t tid;
	
	printf("Main thread function running\n");
  	pthread_create(&tid,NULL ,pth_fun,NULL);
	while(1){
			printf("############\n");
			sleep(2);
		}
	#endif

	/*test compile macro*/
	#if 0
	#error compile reported failure here
	#endif

	/* test variable function*/
	#if 0
	av_log(1, "\33[32mThis function test variable parament function\33[0m\n");
	#endif
	
	return 0;
}


